package com.societyGames.codeAlong.state.codeExercise.view.exerciseSelect
{
import com.societyGames.codeAlong.state.codeExercise.model.ModuleData;
import com.societyGames.codeAlong.user.UserData;
import com.societyGames.flashForms.DockContainer;
import com.societyGames.flashForms.DockType;
import com.societyGames.flashForms.DockUnitType;
import com.societyGames.flashForms.IDialog;
import com.societyGames.flashForms.ListContainer;
import com.societyGames.flashForms.ListItem;
import com.societyGames.flashForms.ProportionContainer;
import com.societyGames.flashForms.TextButton;
import com.societyGames.flashForms.VerticalScrollContainer;
import com.societyGames.flashForms.Window;
import com.societyGames.flashForms.basicSkin.BasicControls;
import com.societyGames.flashForms.basicSkin.SharedTextFields;
import com.societyGames.system.EventObject;

import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;

public class ExerciseSelectDialog extends EventDispatcher implements IDialog
{
  private const _incompleteTextColor:uint = 0x000000;
  private const _completeTextColor:uint = 0x888888;

  private var _completeEventObject:EventObject;
  private var _moduleDataSelectedEventObject:EventObject; //Event
  private var _cancelEventObject:EventObject; //Event

  private var _userData:UserData;
  private var _nextModuleId:uint; //The next module. Helpful for suppressing warnings about skipping levels.

  public function ExerciseSelectDialog(showCancel:Boolean = false, errorDescription:String = null)
  {
    this._completeEventObject = new EventObject(this, "COMPLETE");
    this._moduleDataSelectedEventObject = new EventObject(this, "MODULE_DATA_SELECTED");
    this._cancelEventObject = new EventObject(this, "CANCEL");
    initializeInterface(showCancel, errorDescription);
  }

  public function get moduleDataSelectedEventObject():EventObject //Event
  {
    return this._moduleDataSelectedEventObject;
  }

  public function get cancelEventObject():EventObject
  {
    return this._cancelEventObject;
  }

  public function get selectedModuleData():ModuleData
  {
    if (this._exerciseListContainer.selectedItem != null)
    {
      return ModuleData(this._exerciseListContainer.selectedItem.tag);
    }
    return null;
  }

  /**
   * initialize also tries to scroll to the current selected. To do this
   * call init AFTER the dialog is already sized properly.
   */
  public function initialize(userData:UserData, moduleDatas:Vector.<ModuleData>, currentModuleData:ModuleData = null):void
  {
    this._userData = userData;
    var currentModuleIndex:int;
    for (var index:int = 0; index < moduleDatas.length; index++)
    {
      var moduleData:ModuleData = moduleDatas[index];
      var shouldSelect:Boolean = false;
      if (currentModuleData != null && currentModuleData.id == moduleData.id)
      {
        shouldSelect = true;
      }
      var isBold:Boolean = shouldSelect;
      var textColor:uint = userData.completedExerciseIds.getValue(moduleData.id) ? _completeTextColor : _incompleteTextColor;
      var item:ListItem = BasicControls.createListItem(moduleData.name,
              textColor,
              isBold);
      item.tag = moduleData;
      this._exerciseListContainer.addItem(item);
      //If the module is the current one, select it.
      if (shouldSelect)
      {
        this._exerciseListContainer.selectItem(item);
        this._nextModuleId = moduleData.id;
        currentModuleIndex = index;
      }
    }
    _exerciseVerticalScrollContainer.yPercent = (currentModuleIndex + 0.5) / moduleDatas.length;
  }

  private function isModuleComplete(id:uint):Boolean
  {
    return id < this._userData.currentExerciseId;
  }

  private function okClickCallback(event:Event):void
  {
    /*if (this.selectedModuleData != null)
     {
     if (!isModuleComplete(this.selectedModuleData.id) && this.selectedModuleData.id != this._nextModuleId)
     {
     //TODO: Show a are you sure message.
     }
     }*/
    //TODO: Move this to the OK event of the sure mesage.
    this._completeEventObject.dispatchEvent();
    if (this.selectedModuleData != null)
    {
      this._moduleDataSelectedEventObject.dispatchEvent();
    }
  }

  private function cancelClickCallback(event:Event):void
  {
    this._completeEventObject.dispatchEvent();
    this._cancelEventObject.dispatchEvent();
  }

  //IDialog-->
  public function get completeEventObject():EventObject
  {
    return this._completeEventObject;
  }

  public function get displayObject():DisplayObject
  {
    return this._window;
  }

  //<--IDialog

  private var _window:Window;
  private var _okButton:TextButton;
  private var _cancelButton:TextButton;
  private var _exerciseVerticalScrollContainer:VerticalScrollContainer;
  private var _exerciseListContainer:ListContainer;

  private function initializeInterface(showCancel:Boolean, errorDescriptionHTML:String):void
  {
    this._exerciseListContainer = new ListContainer();

    this._exerciseVerticalScrollContainer = BasicControls.createVerticalScrollContainer();
    this._exerciseVerticalScrollContainer.item = this._exerciseListContainer;

    if (errorDescriptionHTML != null)
    {
      var informationTextField:TextField = SharedTextFields.createHtmlTextField();
      informationTextField.autoSize = TextFieldAutoSize.LEFT;
      informationTextField.multiline = true;
      informationTextField.wordWrap = true;
      informationTextField.mouseEnabled = false;
      informationTextField.border = true;
      informationTextField.borderColor = 0x0;
      informationTextField.htmlText = errorDescriptionHTML;

      var informationProportionContainer:ProportionContainer = new ProportionContainer();
      informationProportionContainer.item = informationTextField;

      var informationVerticalScrollContainer:VerticalScrollContainer = BasicControls.createVerticalScrollContainer();
      informationVerticalScrollContainer.item = informationProportionContainer;
    }

    var selectMissionTextField:TextField = SharedTextFields.createSingleLineTextField("Select Mission:");
    var textFormat:TextFormat = selectMissionTextField.defaultTextFormat;
    textFormat.bold = true;
    selectMissionTextField.setTextFormat(textFormat, -1, -1);

    this._okButton = BasicControls.createTextButton(okClickCallback);
    this._okButton.text = "OK";

    var buttonDockContainer:DockContainer = new DockContainer();
    if (showCancel)
    {
      this._cancelButton = BasicControls.createTextButton(cancelClickCallback);
      this._cancelButton.text = "Cancel";
      buttonDockContainer.addItem(this._okButton, DockType.left, DockUnitType.percent, 0.5);
      buttonDockContainer.addItem(this._cancelButton, DockType.fill, DockUnitType.percent, 1.0);
    }
    else
    {
      buttonDockContainer.addItem(this._okButton, DockType.fill, DockUnitType.percent, 1.0);
    }

    this._window = BasicControls.createWindow("Mission Select");
    this._window.width = 260;
    this._window.height = 500;

    var dockContainer:DockContainer = new DockContainer();
    if (errorDescriptionHTML != null)
    {
      informationProportionContainer.width = this._window.innerWidth;
      dockContainer.addItem(informationVerticalScrollContainer, DockType.bottom, DockUnitType.pixel, informationTextField.height);
    }
    dockContainer.addItem(selectMissionTextField, DockType.top, DockUnitType.pixel, selectMissionTextField.height);
    dockContainer.addItem(buttonDockContainer, DockType.bottom, DockUnitType.pixel, BasicControls.TEXT_BUTTON_HEIGHT); //Pin at natural height of buttons.
    dockContainer.addItem(this._exerciseVerticalScrollContainer, DockType.fill, DockUnitType.percent, 1.0);

    this._window.item = dockContainer;
  }
}
}